﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using Tools;
using Wp.DAL;
using TagCloud;

namespace Wp.BLL
{
    /// <summary>
    /// Summary description for Per_ActionsRoles
    /// </summary>
    public class TblTermTaxonomyManager : TblTermTaxonomyManagerBase
    {
        public TblTermTaxonomyManager()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public static TblTermTaxonomyManager CreateInstant()
        {
            return new TblTermTaxonomyManager();
        }
        public void UpdateParentOfChild(int Id, int ParentId)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", Id));
            ls.Add(new SqlParameter("@ParentId", ParentId));
            SqlParameter[] sqlparam = ls.ToArray();
            SqlHelper.ExecuteNonQuery(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_UpdateParentOfChildTblTermTaxonomy", sqlparam);
        }
        public int InsertTag(string Name, string Slug, int ParentId, string Des, string Taxonomy)
        {
            try
            {
                if (Name.Equals(""))
                {
                    return 0;
                }
                int TermId = 0;
                if (Slug.Equals(string.Empty))
                    Slug = UnicodeConversion.CreateInstant().GetStringId(Name);
                TblTermsEntity item = TblTermsManager.CreateInstant().SelectOneSlug(Slug);
                if (item == null)
                    TermId = TblTermsManager.CreateInstant().InsertUpdate(-1, Name, Slug);
                else TermId = item.Id;
                int result = wp_CheckTblTermTaxonomyUnique(0, TermId, Taxonomy);
                if (result > 0)
                    return -2;//exist
                return TblTermTaxonomyManager.CreateInstant().InsertUpdate(-1, TermId, Taxonomy, Des, ParentId, 0);
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
        public int InsertPostTag(int PostId, string Name, string Taxonomy)
        {
            try
            {
                if (Name.Equals(""))
                {
                    return 0;
                }
                int TermId = 0;
                string Slug = UnicodeConversion.CreateInstant().GetStringId(Name);
                TblTermsEntity item = TblTermsManager.CreateInstant().SelectOneSlug(Slug);
                if (item == null)
                    TermId = TblTermsManager.CreateInstant().InsertUpdate(-1, Name, Slug);
                else TermId = item.Id;
                int result = wp_CheckTblTermTaxonomyUnique(0, TermId, Taxonomy);
                if (result == 0)
                {
                    result = TblTermTaxonomyManager.CreateInstant().InsertUpdate(-1, TermId, Taxonomy, "", -1, 0);
                }
                TblTermRelationshipsManager.CreateInstant().Delete(PostId, result);
                TblTermRelationshipsManager.CreateInstant().InsertUpdate(PostId, result);
                return result;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
        public int UpdateTag(int Id, string Name, string Slug, int ParentId, string Des, string Taxonomy)
        {
            try
            {
                if (Name.Equals(""))
                {
                    return 0;
                }
                int TermId = 0;
                if (Slug.Equals(string.Empty))
                    Slug = UnicodeConversion.CreateInstant().GetStringId(Name);
                TblTermsEntity item = TblTermsManager.CreateInstant().SelectOneSlug(Slug);
                if (item == null)
                    TermId = TblTermsManager.CreateInstant().InsertUpdate(-1, Name, Slug);
                else
                {
                    TblTermsManager.CreateInstant().InsertUpdate(item.Id, Name, Slug);
                    TermId = item.Id;
                }
                int result = wp_CheckTblTermTaxonomyUnique(Id, TermId, Taxonomy);
                if (result != 0)
                    return -2;//exist
                return TblTermTaxonomyManager.CreateInstant().InsertUpdate(Id, TermId, Taxonomy, Des, ParentId, 0);
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
        public List<TblTermTaxonomyEntity> SelectTblTermsTaxonomyGetChildFormat(int? Id, string Taxonomy)
        {
            List<TblTermTaxonomyEntity> lsresult = new List<TblTermTaxonomyEntity>();
            List<SqlParameter> ls = new List<SqlParameter>();
            if (Id.HasValue)
                ls.Add(new SqlParameter("@Id", Id));
            ls.Add(new SqlParameter("@Taxonomy", Taxonomy));
            SqlParameter[] sqlparam = ls.ToArray();
            DataTable dt = SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblTermsTaxonomyGetChildFormat", sqlparam);

            if (dt != null && dt.Rows.Count > 0)
            {
                TblTermTaxonomyEntity item = TblTermTaxonomyEntity.CreateInstant();
                for (int i = 0; i < dt.Rows.Count; i++)
                    lsresult.Add(item.GetTblTermTaxonomyEntity(dt.Rows[i]));
            }
            return lsresult;
        }
        public List<TblTermTaxonomyEntity> SelectTblTermsTaxonomyGetChildFormat(int? Id, string Taxonomy, int PageSize, int PageIndex, out int TotalRow)
        {
            TotalRow = 0;
            List<TblTermTaxonomyEntity> lsresult = new List<TblTermTaxonomyEntity>();
            List<SqlParameter> ls = new List<SqlParameter>();
            SqlParameter sp = new SqlParameter("@TotalRow", TotalRow);
            sp.Direction = System.Data.ParameterDirection.Output;
            ls.Add(sp);
            if (Id.HasValue)
                ls.Add(new SqlParameter("@Id", Id));
            ls.Add(new SqlParameter("@Taxonomy", Taxonomy));
            ls.Add(new SqlParameter("@PageSize", PageSize));
            ls.Add(new SqlParameter("@PageIndex", PageIndex));

            SqlParameter[] sqlparam = ls.ToArray();
            DataTable dt = SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblTermsTaxonomyGetChildFormat", sqlparam);
            TotalRow = int.Parse(sqlparam[0].Value.ToString());
            if (dt != null && dt.Rows.Count > 0)
            {
                TblTermTaxonomyEntity item = TblTermTaxonomyEntity.CreateInstant();
                for (int i = 0; i < dt.Rows.Count; i++)
                    lsresult.Add(item.GetTblTermTaxonomyEntity(dt.Rows[i]));
            }
            return lsresult;
        }
        public int wp_CheckTblTermTaxonomyUnique(int? Id, int TermId, string Taxonomy)
        {
            int result = 0;
            List<SqlParameter> ls = new List<SqlParameter>();
            if (Id.HasValue)
                ls.Add(new SqlParameter("@Id", Id));
            else
                ls.Add(new SqlParameter("@Id", 0));
            ls.Add(new SqlParameter("@TermId", TermId));
            ls.Add(new SqlParameter("@Taxonomy", Taxonomy));
            SqlParameter[] sqlparam = ls.ToArray();
            DataTable dt = SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_CheckTblTermTaxonomyUnique", sqlparam);
            if (dt != null && dt.Rows.Count > 0)
            {
                result = dt.Rows[0]["Id"].ToInt();
            }
            return result;
        }
        public List<Tag> SelectTagTopCount(string Taxonomy, int Top)
        {
            List<Tag> lsresult = new List<Tag>();
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Taxonomy", Taxonomy));
            ls.Add(new SqlParameter("@Top", Top));
            SqlParameter[] sqlparam = ls.ToArray();
            DataTable dt = SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblTermTaxonomyTopCount", sqlparam);
            if (dt != null && dt.Rows.Count > 0)
            {
                TblTermTaxonomyEntity item = TblTermTaxonomyEntity.CreateInstant();
                for (int i = 0; i < dt.Rows.Count; i++)
                    lsresult.Add(item.GetTagEntity(dt.Rows[i]));
            }
            return lsresult;
        }
    }
}
